package com.mi.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mi.entity.ResponseMsg;
import com.mi.entity.ProductCategoryEntity;
import com.mi.service.ProductCategoryService;
import com.mi.util.UUIDUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ProductCategoryController {

    @Reference(check = false)
    ProductCategoryService productCategoryService;

    //会员列表
    @RequestMapping("productCategoryList")
    public String productCategoryList(ProductCategoryEntity productCategoryEntity, HttpServletRequest request){
        productCategoryEntity.setPc_rank(1);
        ArrayList<ProductCategoryEntity> productCategoryEntities1 = new ArrayList<>();
        List<ProductCategoryEntity> productCategoryEntities = productCategoryService.queryProductCategorys(productCategoryEntity);
        productCategoryEntity.setPc_rank(0);
        for (int i = 0; i < productCategoryEntities.size(); i++) {
            productCategoryEntities1.add(productCategoryEntities.get(i));
            productCategoryEntity.setPpc_id(productCategoryEntities.get(i).getPc_id());
            productCategoryEntities1.addAll(productCategoryService.queryProductCategorys(productCategoryEntity));
        }
        request.setAttribute("productCategorys",productCategoryEntities1);
        return "productCategoryList";
    }
    //删除会员
    @RequestMapping("delProductCategory")
    @ResponseBody
    public ResponseMsg delProductCategory(String ids) {
        ResponseMsg rm = new ResponseMsg();
        String[] pc_ids;
        if (ids.contains(",")) {
            pc_ids = ids.split(",");
        } else {
            pc_ids = new String[]{ids};
        }
        boolean b = productCategoryService.delProductCategory(pc_ids);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("删除失败");
        }
        return rm;
    }

    //会员状态更改
    @RequestMapping("updProductCategoryStatus")
    @ResponseBody
    public ResponseMsg updProductCategoryStatus(ProductCategoryEntity productCategoryEntity) {
        ResponseMsg rm = new ResponseMsg();
        boolean b = productCategoryService.updProductCategoryStatus(productCategoryEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到修改会员界面
    @RequestMapping("productCategoryEdit")
    public String productCategoryEdit(ProductCategoryEntity productCategoryEntity,HttpServletRequest request){
        ProductCategoryEntity productCategoryEntity1 = productCategoryService.queryProductCategory(productCategoryEntity);
        if(productCategoryEntity1 != null){
            request.setAttribute("pc",productCategoryEntity1);
            return "productCategoryEdit";
        }else{
            return "error";
        }
    }

    //修改会员
    @RequestMapping("updProductCategory")
    @ResponseBody
    public ResponseMsg updproductCategory(MultipartFile myfile, ProductCategoryEntity productCategoryEntity, HttpServletRequest request){
        ResponseMsg rm = new ResponseMsg();
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String productCategoryImage = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productCategoryIcon";
            File file = new File(productCategoryImage);
            if(!file.exists()){
                file.mkdirs();
            }
            productCategoryImage = productCategoryImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(productCategoryImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                productCategoryEntity.setPc_image(File.separator+"admin"+File.separator+productCategoryImage.substring(productCategoryImage.lastIndexOf(File.separator,productCategoryImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        boolean b = productCategoryService.updProductCategory(productCategoryEntity);
        if (b) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("修改失败");
        }
        return rm;
    }

    //跳转到添加会员界面
    @RequestMapping("productCategoryAdd")
    public String productCategoryAdd(String pc_id,HttpServletRequest request){
        request.setAttribute("ppc_id",pc_id);
        return "productCategoryAdd";
    }

    //添加会员
    @RequestMapping("addProductCategory")
    @ResponseBody
    public ResponseMsg addProductCategory(MultipartFile myfile,ProductCategoryEntity productCategoryEntity){
        ResponseMsg rm = new ResponseMsg();
        if(myfile.getOriginalFilename()!=null&&myfile.getOriginalFilename()!=""){
            String productCategoryImage = File.separator+"root"+File.separator+"static"+File.separator+"admin"+File.separator+"productCategoryIcon";
            File file = new File(productCategoryImage);
            if(!file.exists()){
                file.mkdirs();
            }
            productCategoryImage = productCategoryImage + File.separator+ UUIDUtil.getUUID()+myfile.getOriginalFilename().substring(myfile.getOriginalFilename().lastIndexOf("."));
            try {
                FileOutputStream fos = new FileOutputStream(productCategoryImage);
                InputStream is = myfile.getInputStream();
                FileCopyUtils.copy(is, fos);
                fos.close();
                is.close();
                productCategoryEntity.setPc_image(File.separator+"admin"+File.separator+productCategoryImage.substring(productCategoryImage.lastIndexOf(File.separator,productCategoryImage.lastIndexOf(File.separator)-1)+1));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(productCategoryEntity.getPpc_id()>0){
            productCategoryEntity.setPc_rank(2);
        }else{
            productCategoryEntity.setPc_rank(1);
        }
        boolean regist = productCategoryService.addProductCategory(productCategoryEntity);
        if (regist) {
            rm.setResult("0000");
        } else {
            rm.setResult("0001");
            rm.setResultMsg("添加失败");
        }
        return rm;
    }

}
